using System;
using System.Data;
using Common.Enumerations;
using Sanelib.DataOperation;
using Sanelib.DataOperation.Attribute;
using Sanelib.DataOperation.Base;
using Sanelib.Dto.Enumerations;

namespace Core.Domain.Model
{
    [EntityName("Member")]
    public class Member : AuditedEntity
    {
        public string TagId { get; set; }
        public string Code { get; set; }
        public DateTime? DateOfBirth { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Mobile { get; set; }
        public string ScreenMessage { get; set; }
        public DateTime? ValidFrom { get; set; }
        public DateTime? ValidTo { get; set; }
        public bool? SendSms { get; set; }
        public bool? SendEmail { get; set; }
        public bool? UsePrint { get; set; }
        public bool? Block { get; set; }

        public string PasswordHash { get; set; }
        public string PasswordSalt { get; set; }

        public int FailedAttemptCount { get; set; }
        public Role Role { get; set; }
        public UserStatus UserStatus { get; set; }

        public string PasswordRetrievalToken { get; set; }
        public DateTime? LastLoginTime { get; set; }
        public DateTime? PasswordRetrievalTokenExpirationDate { get; set; }
        public DateTime? LastPasswordChangedDate { get; set; }

        public string Session { get; set; }


        public override void From(IDataReader dataReader)
        {
            base.From(dataReader);

            TagId = dataReader.ReadNullSafeString("TagId");
            Code = dataReader.ReadNullSafeString("Code");
            DateOfBirth = dataReader.ReadNullSafeDateTime("DateOfBirth");
            Address = dataReader.ReadNullSafeString("Address");
            Email = dataReader.ReadNullSafeString("Email");
            Mobile = dataReader.ReadNullSafeString("Mobile");
            ScreenMessage = dataReader.ReadNullSafeString("ScreenMessage");
            ValidFrom = dataReader.ReadNullSafeDateTime("ValidFrom");
            ValidTo = dataReader.ReadNullSafeDateTime("ValidTo");
            SendSms = dataReader.ReadNullSafeBool("SendSms");
            SendEmail = dataReader.ReadNullSafeBool("SendEmail");
            UsePrint = dataReader.ReadNullSafeBool("UsePrint");
            Block = dataReader.ReadNullSafeBool("Block");
            PasswordHash = dataReader.ReadNullSafeString("PasswordHash");
            PasswordSalt = dataReader.ReadNullSafeString("PasswordSalt");
            FailedAttemptCount = dataReader.ReadNullSafeInt("FailedAttemptCount").GetValueOrDefault();
            Role = Role.FromValue(dataReader.ReadNullSafeString("RoleValue"));
            UserStatus = UserStatus.FromValue(dataReader.ReadNullSafeString("UserStatusValue"));
            PasswordRetrievalToken = dataReader.ReadNullSafeString("PasswordRetrievalToken");
            LastLoginTime = dataReader.ReadNullSafeDateTime("LastLoginTime");
            PasswordRetrievalTokenExpirationDate = dataReader.ReadNullSafeDateTime("PasswordRetrievalTokenExpirationDate");
            LastPasswordChangedDate = dataReader.ReadNullSafeDateTime("LastPasswordChangedDate");
            Session = dataReader.ReadNullSafeString("Session");
        }

        public override void To(System.Data.SqlClient.SqlCommand cmd)
        {
            base.To(cmd);
            cmd.Parameters.Add("@TagId", SqlDbType.VarChar).Value = (object)TagId ?? DBNull.Value;
            cmd.Parameters.Add("@Code", SqlDbType.VarChar).Value = Code;
            cmd.Parameters.Add("@DateOfBirth", SqlDbType.DateTime).Value = (object)DateOfBirth ?? DBNull.Value;
            cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = Address;
            cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email;
            cmd.Parameters.Add("@Mobile", SqlDbType.VarChar).Value = Mobile;
            cmd.Parameters.Add("@ScreenMessage", SqlDbType.VarChar).Value = (object)ScreenMessage ?? DBNull.Value;
            cmd.Parameters.Add("@ValidFrom", SqlDbType.DateTime).Value = (object)ValidFrom ?? DBNull.Value;
            cmd.Parameters.Add("@ValidTo", SqlDbType.DateTime).Value = (object)ValidTo ?? DBNull.Value;
            cmd.Parameters.Add("@SendSms", SqlDbType.Bit).Value = (object)SendSms ?? DBNull.Value;
            cmd.Parameters.Add("@SendEmail", SqlDbType.Bit).Value = (object)SendEmail ?? DBNull.Value;
            cmd.Parameters.Add("@UsePrint", SqlDbType.Bit).Value = (object)UsePrint ?? DBNull.Value;
            cmd.Parameters.Add("@Block", SqlDbType.Bit).Value = (object)Block ?? DBNull.Value;
            cmd.Parameters.Add("@PasswordHash", SqlDbType.VarChar).Value = (object)PasswordHash ?? DBNull.Value;
            cmd.Parameters.Add("@PasswordSalt", SqlDbType.VarChar).Value = (object)PasswordSalt ?? DBNull.Value;
            cmd.Parameters.Add("@FailedAttemptCount", SqlDbType.Int).Value = FailedAttemptCount;
            cmd.Parameters.Add("@RoleValue", SqlDbType.Char, 2).Value = BaseEnumeration.GetDbNullSafe(Role) ?? DBNull.Value;
            cmd.Parameters.Add("@UserStatusValue", SqlDbType.Char, 2).Value = BaseEnumeration.GetDbNullSafe(UserStatus) ?? DBNull.Value;
            cmd.Parameters.Add("@PasswordRetrievalToken", SqlDbType.VarChar).Value = (object)PasswordRetrievalToken ?? DBNull.Value;
            cmd.Parameters.Add("@LastLoginTime", SqlDbType.DateTime).Value = (object)LastLoginTime ?? DBNull.Value;
            cmd.Parameters.Add("@PasswordRetrievalTokenExpirationDate", SqlDbType.DateTime).Value = (object)PasswordRetrievalTokenExpirationDate ?? DBNull.Value;
            cmd.Parameters.Add("@LastPasswordChangedDate", SqlDbType.DateTime).Value = (object)LastPasswordChangedDate ?? DBNull.Value;
            cmd.Parameters.Add("@Session", SqlDbType.VarChar).Value = (object)Session ?? DBNull.Value;
        }
    }
}